<?php
namespace Admin\Controller;
use Think\Controller;
class InfoTuController extends CommonController
{	
	//捕鼠盒
	public function one(){
		session('page_size',$_GET['page_size']);
		$page_no = I('get.p') ? I('get.p') : 1;// 分页
        $page_size = I('get.page_size') ? I('get.page_size') : 10; // 分页

		$data = M('bait_log a')
			->where('')
			->field('a.number,a.device_id,b.name')
			->join('left join zm_device b on a.device_id=b.id')
			->page($page_no, $page_size)
			->select();
		$count = M('bait_log a')->count();
		foreach ($data as $k => $v) {
			$arr[] = (float)$v['number'];
			$arr1[] = $v['name'];
		}

		$data = json_encode($arr1);
		$list = json_encode($arr);

		$Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
		$this->assign("list", $list);
		$this->assign("data", $data);

		$this->assign('page', $show);
        $this->assign("count", $count);
		$this->display();
	}

	//飞虫
	public function two(){
		session('page_size',$_GET['page_size']);//列表显示多少条

		$time = strtotime(date('Y-m-d',strtotime('-30 day')));// 默认30天

		$begin_time = I('get.begin_time') ? strtotime(I('get.begin_time')) : $time;
		$end_time   = I('get.end_time') ? strtotime(I('get.end_time')) : time();
		$timediff = $end_time-$begin_time;
      	$days = intval($timediff/86400);// 相隔多少天用来计算指数

      	$page_no = I('get.p') ? I('get.p') : 1;// 分页
        $page_size = I('get.page_size') ? I('get.page_size') : 10; // 分页

      	$maps['add_time'] = array(array('gt',$begin_time),array('lt',$end_time));

		$cate_id = C('DEV_LOG_1');
		$animal_id = M('device_cate')->where('id='.$cate_id)->getField('pest');

		$map['id']=array('in',$animal_id);
		$animal_name=M('animal')->where($map)->field('id,name')->select();
		
		$data = M('device a')
			->where('a.cate_id='.$cate_id)
			->field('a.id,a.name')
			// ->field('a.id,a.name,b.number,b.device_id,b.animal_id,sum(b.number)')
			// ->join('left join zm_feichong_log b on a.id=b.device_id')
			// ->group('id')
			->page($page_no, $page_size)
			->select();

		foreach ($data as $k => $v) {
			// $data[$k]['feichong_log'] = M('feichong_log')->where('device_id='.$v['id'])->select();

			foreach ($animal_name as $key => $val) {
				$data[$k][$val['name']] = M('feichong_log')
					->where($maps)
					->where('device_id='.$v['id'].' AND animal_id='.$val['id'])
					->getField('sum(number) as sum_number');

				$data[$k]['sum'] += $data[$k][$val['name']];
			}
			
		}

		// dump($data);exit();
		//  关联查询后的数据 格式化 一对多查询体
		// foreach ($data as $key => $value) {
		// 	if ($value['device_id']) {
		// 		$list[$value['id']]['join'][]=array('animal_id'=>$value['animal_id'],'number'=>$value['number']);
		// 	}else{
		// 		$list[$value['id']]['join'] = null;
		// 	}
		// 	$list[$value['id']]['name'] = $value['name'];
		// 	$list[$value['id']]['id'] = $value['id'];
		// }

		foreach ($animal_name as $k => $v) {
			$arr[] = $v['name'];// 动物名字
		}

		$safe_num_fei = M('configs')->where("ename='safe_num_fei'")->getField('val');
		foreach ($data as $key => $value) {
			$id[] = (int)$value['id'];
			$safe[] = (int)$safe_num_fei;
			$feinum[] = (float)sprintf('%.2f',$value['sum']/$days);//飞虫总指数
			// $animal1[]=$value[$arr[0]];
			for ($i=0; $i <count($arr) ; $i++) { 
				$animal[$i][] = (int)$value[$arr[$i]];
			}
		}
		// dump($animal[1]);exit();
		$id = json_encode($id);
		$safe = json_encode($safe);
		$feinum = json_encode($feinum);

		for ($i=0; $i <count($arr) ; $i++) { 
			$animal[$i] = json_encode($animal[$i]);
		}


		
		// dump($animal);exit();
		$avg = M('configs')->where("ename='fei_sum'")->getField('name');
		$fei = M('configs')->where("ename='safe_num_fei'")->getField('name');

		$this->assign("safe", $safe); // 安全指数
		$this->assign("feinum", $feinum);

		$this->assign("animal_name", $arr);
		$this->assign("data", $data);
		$this->assign("ids", $id);
		$this->assign("avg", $avg);
		$this->assign("fei", $fei);
		$this->assign("animal", $animal);

		$this->assign('page', $show);
        $this->assign("count", $count);

        $this->assign("begin_time", $begin_time);
        $this->assign("end_time", $end_time);

		$this->display();
	}

	//粘鼠板
	public function mousePlate(){
		$data = M('glue_log a')
			->field('a.number,a.device_id,b.name')
			->join('left join zm_device b on a.device_id=b.id')
			->select();

		foreach ($data as $k => $v) {
			$arr[] = (float)$v['number'];
			$arr1[] = $v['name'];
		}

		$data = json_encode($arr1);
		$list = json_encode($arr);

		$this->assign("list", $list);
		$this->assign("data", $data);
		$this->display();
	}

	//机械捕鼠器
	public function mouseIron(){
		$data = M('mousetrap_log a')
			->field('a.number,a.device_id,b.name')
			->join('left join zm_device b on a.device_id=b.id')
			->select();

		foreach ($data as $k => $v) {
			$arr[] = (float)$v['number'];
			$arr1[] = $v['name'];
		}

		$data = json_encode($arr1);
		$list = json_encode($arr);

		$this->assign("list", $list);
		$this->assign("data", $data);
		$this->display();
	}

	//爬虫板
	public function mouseBox1(){
		$data = M('reptile_log a')
			->field('a.number,a.device_id,b.name')
			->join('left join zm_device b on a.device_id=b.id')
			->select();
		// dump($data);exit();
		foreach ($data as $k => $v) {
			$arr[] = (float)$v['number'];
			$arr1[] = $v['name'];
		}

		$data = json_encode($arr1);
		$list = json_encode($arr);

		$this->assign("list", $list);
		$this->assign("data", $data);
		$this->display();
	}

	public function mouseBox(){
		session('page_size',$_GET['page_size']);

		$page_no = I('get.p') ? I('get.p') : 1;
        $page_size = I('get.page_size') ? I('get.page_size') : 10;

        $cate_id = C('DEV_LOG_5');
        $animal_id = M('device_cate')->where('id='.$cate_id)->getField('pest');

		$data = M('device a')
			->where('a.cate_id='.$cate_id)
			->field('a.id,a.name,area')
			->page($page_no, $page_size)
			->select();

		foreach ($data as $k => $v) {
			$data[$k]['yue1'] = M('reptile_log')->where(array('date_m'=>1))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue2'] = M('reptile_log')->where(array('date_m'=>2))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue3'] = M('reptile_log')->where(array('date_m'=>3))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue4'] = M('reptile_log')->where(array('date_m'=>4))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue5'] = M('reptile_log')->where(array('date_m'=>5))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue6'] = M('reptile_log')->where(array('date_m'=>6))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue7'] = M('reptile_log')->where(array('date_m'=>7))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue8'] = M('reptile_log')->where(array('date_m'=>8))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue9'] = M('reptile_log')->where(array('date_m'=>9))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue10'] = M('reptile_log')->where(array('date_m'=>10))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue11'] = M('reptile_log')->where(array('date_m'=>11))->where('device_id='.$v['id'])->getField('sum(number) as number');
			$data[$k]['yue12'] = M('reptile_log')->where(array('date_m'=>12))->where('device_id='.$v['id'])->getField('sum(number) as number');
		}

		// foreach ($data as $key => $value) {
		// 	$data[$key]['$key']=M('bait_log')->field('number')->select();
		// }
		// dump($data);exit();
		$count =M('device')->where('cate_id='.$cate_id)->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();

		$this->assign('page', $show);
        $this->assign("count", $count);
        $this->assign("data", $data);
		$this ->display();
	}

	public function mouseRep(){
		session('page_size',$_GET['page_size']);

		$time = strtotime(date('Y-m-d',strtotime('-30 day')));

		$begin_time = I('get.begin_time') ? strtotime(I('get.begin_time')) : $time;
		$end_time   = I('get.end_time') ? strtotime(I('get.end_time')) : time();
		$timediff = $endtime-$begin_time;
      	$days = intval($timediff/86400);// 相隔多少天用来计算指数

		$page_no = I('get.p') ? I('get.p') : 1;// 分页
        $page_size = I('get.page_size') ? I('get.page_size') : 10; // 分页

        $maps['add_time'] = array(array('gt',$begin_time),array('lt',$end_time));

        $cate_id = C('DEV_LOG_5');
        $animal_id = M('device_cate')->where('id='.$cate_id)->getField('pest');//该分类下的物种

		$map['id']=array('in',$animal_id);
		$animal_name=M('animal')->where($map)->field('id,name,ename')->select();// 每个物种对应的id

		// 二维码
		$count = M('device a')->where('a.cate_id='.$cate_id)->count(); 
		$data = M('device a')
			->where('a.cate_id='.$cate_id)
			->field('a.id,a.name,a.area')
			->page($page_no, $page_size)
			->select();

		foreach ($data as $k => $v) 
		{
			// $data[$k]['feichong_log'] = M('feichong_log')->where('device_id='.$v['id'])->select();

			foreach ($animal_name as $key => $val) 
			{
				$data[$k][$val['ename']] = M('reptile_log')
					->where($maps)
					->where('device_id='.$v['id'].' AND animal_id='.$val['id'])
					->getField('sum(number) as sum_number');

				$data[$k]['sum'] += $data[$k][$val['ename']];
			}
			
		}
		// dump($data);exit();
		foreach ($animal_name as $k => $v) {
			$arr1[] = $v['name'];// 动物名字
			$arr[] = $v['ename'];// 动物名字
		}

		foreach ($data as $key => $value) {
			$id[] = (int)$value['id'];

			for ($i=0; $i <count($arr) ; $i++) { 
				$animal[$i][] = (int)$value[$arr[$i]];
			}
		}
		// dump($animal[1]);exit();
		$id = json_encode($id);


		for ($i=0; $i <count($arr) ; $i++) { 
			$animal[$i] = json_encode($animal[$i]);
		}
		// dump($animal);exit();
		$Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
		$this->assign("animal_name", $arr1);
		$this->assign("ids", $id);
		$this->assign("animal", $animal);


		$this->assign('page', $show);
        $this->assign("count", $count);

        $this->assign("begin_time", $begin_time);
        $this->assign("end_time", $end_time);
        
		$this->display();
	}

	public function search(){
		$data = 1;
		if ($data) {
            $this->success('ok');
        }else{
            $this->error('添加失败');
        }
	}

	public function test(){

		$data = array(
			array('id'=>1,'num'=>2,'uid'=>1),
			array('id'=>2,'num'=>2,'uid'=>1),
			array('id'=>3,'num'=>2,'uid'=>2),
			array('id'=>4,'num'=>2,'uid'=>2),
			array('id'=>5,'num'=>2,'uid'=>3),
			);
		foreach ($data as $k => $v) {
			$da[$v['uid']]+=$v['num'];
		}
		dump($da);exit();
	}
}